<?php

    require ( 'init.inc.php' );
    require ( 'checkadmin.inc.php');
    require ( __CLASS_PATH . 'gd_image.class.php');

	$action = $_REQUEST['action'];
	$fName = $_REQUEST['fName'];
	$cName = $_REQUEST['cName'];
    $Usewartermark = $_REQUEST['Usewartermark'];
    
	$IsImage = $_REQUEST['IsImage']=="True" ? "True" : "False";
    
    if ($action=="upload") {

        $up_file_dir = __UPLOAD_PATH ."month_" .date("ym")."/";
        $ext_name = get_file_ext($_FILES["upfile"]["name"]);
		/*if($ext_name=="mp3"){
			$up_file_dir=__UPLOAD_PATH."media/"."month_".date("ym")."/";
			}*/
        $uploadfile = $up_file_dir.date("mdhis").".".$ext_name;
        $uploadfile_default = $uploadfile;  //
        $upload_ok = false;

        if ($IsImage=="True" && !IsImgFile($ext_name))
            $feedmsg = "无效的图片格式文件!";
        elseif (!IsValidFile($ext_name))
            $feedmsg = "禁止上传扩展名为 \'$ext_name\' 的文件！请与管理员联系！";
        elseif($_FILES["upfile"]["size"]>$upfilesize)
            $feedmsg = "文件大小超出限制!";
        
        else {
            if(!is_dir($up_file_dir)){ //检查上传目录是否存在，不存在则创建
                if (!mkdir($up_file_dir,0777)) {
                    $feedmsg = "权限不足无法创建目录: $up_file_dir,请联系管理员将目录设置为可写!";
                    alert($feedmsg);
                }
            }
            
            if (move_uploaded_file($_FILES["upfile"]["tmp_name"], $uploadfile)) {
               $upload_ok = true;
               $feedmsg = "文件上传成功!";
               if (in_array($ext_name , array ('gif', 'jpg', 'jpeg', 'bmp', 'png'))) {
                   $GDImage = new GDImage;
                   $load = $GDImage->loadFile($uploadfile);
                   if($load){
                       $GDImage->mkThumb($maxpicwidth, $maxpicheight);
                       $GDImage->build($uploadfile);
                       $feedmsg .= " 图片自动缩放成功! \\n宽: {$maxpicwidth} 像素(px)\\n高: {$maxpicheight} 像素(px)";
                   }
                   else $feedmsg .= " 图片自动缩放失败!";
               }
               $uploadfile = str_replace( __SITE_ROOT, "/", $uploadfile);
            }
            else
               $feedmsg = "文件上传失败!";
            //图片水印
            //echo $uploadfile_default.$Usewartermark.$watermark; exit;
            if($Usewartermark==1&&$watermark<>'none'){
               // echo "ddd"; exit;
                if($watermark=='text'){
                    imageWaterMark ( $uploadfile_default , $watermark_position , "" , $watermark_text , $textFont = 5 , $watermark_text_color );
                }
                else{
                    $watermark_image = '..'.$watermark_image;   //水印图片相对位置
                    //echo $watermark_image; exit;
                    imageWaterMark ( $uploadfile_default , $watermark_position , $watermark_image , "", $textFont = 5 , "");
                }
            }
        }

        if ($is_test_space) $uploadfile = "/".$test_space_name.$uploadfile; // 如果为测试空间服务器，重新设定文件路径

        if (!empty($feedmsg)) alert($feedmsg);
        // 执行最后的JS代码
        if ($upload_ok) doJS("opener.document.$fName.$cName.value='$uploadfile';self.close()");
        else goto("uploadfile.php?fName=$fName&cName=$cName");
    }
    else {
        $tpl->assign("fName",$fName);
        $tpl->assign("cName",$cName);
        $tpl->assign("IsImage",$IsImage);
        $tpl->assign("Usewartermark",$Usewartermark);
        $tpl->display('uploadfile.htm');
    }
?>